package com.developer.platform.infrastructure.client;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.developer.platform.infrastructure.client.DingDingClient.DingDingContent.At;
import com.developer.platform.infrastructure.client.DingDingClient.DingDingContent.Content;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-12-29
 */
@Component
public class DingDingClient {

  private final DingDingSign dingDingSign;

  public DingDingClient(
      DingDingSign dingDingSign) {
    this.dingDingSign = dingDingSign;
  }

  public void send(String url, String secret, String content, List<String> phones){
    DingDingContent dingDingContent = new DingDingContent();
    dingDingContent.setText(new Content(content));
    dingDingContent.setAt(new At(phones));
    HttpRequest.post(dingDingSign.url(url, secret))
        .body(JSONUtil.toJsonStr(dingDingContent))
        .execute().body();
  }


  public static class DingDingContent{
    private String msgtype = "text";
    private Content text;
    private At at;

    public String getMsgtype() {
      return msgtype;
    }

    public void setMsgtype(String msgtype) {
      this.msgtype = msgtype;
    }

    public Content getText() {
      return text;
    }

    public void setText(
        Content text) {
      this.text = text;
    }

    public At getAt() {
      return at;
    }

    public void setAt(At at) {
      this.at = at;
    }

    static class Content{
      private String content;

      public Content(String content) {
        this.content = content;
      }

      public String getContent() {
        return content;
      }

      public void setContent(String content) {
        this.content = content;
      }
    }

    static class At{
      private List<String> atMobiles;
      private boolean isAtAll;

      public At(List<String> atMobiles) {
        this.atMobiles = atMobiles;
        this.isAtAll = false;
      }

      public List<String> getAtMobiles() {
        return atMobiles;
      }

      public void setAtMobiles(List<String> atMobiles) {
        this.atMobiles = atMobiles;
      }

      public boolean isAtAll() {
        return isAtAll;
      }

      public void setAtAll(boolean atAll) {
        isAtAll = atAll;
      }
    }

  }

}
